package com.cslg.ac.demo.service.post;

import com.cslg.ac.demo.constant.ResponseCode;
import com.cslg.ac.demo.dao.host.HostDAO;
import com.cslg.ac.demo.dao.host.po.HostPO;
import com.cslg.ac.demo.dao.post.HostEvaluationDAO;
import com.cslg.ac.demo.dao.post.PostDAO;
import com.cslg.ac.demo.dao.post.po.HostEvaluationPO;
import com.cslg.ac.demo.dao.post.po.PostPO;
import com.cslg.ac.demo.entity.post.HostEvaluation;
import com.cslg.ac.demo.entity.post.Post;
import com.cslg.ac.demo.exception.ParamIllegalException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class PostService {
    @Autowired
    PostDAO postDAO;
    @Autowired
    HostEvaluationDAO hostEvaluationDAO;
    @Autowired
    HostDAO hostDAO;
    @Value("${teamType}")
    int teamType;
    @Value("${personType}")
    int personType;
    @Value("${trendType}")
    int trendType;
    @Value("${commentType}")
    int commentType;
    @Value("${evaluationType}")
    int evaluationType;


    public Integer getUserByPost(int postId){
        return postDAO.selectUserByPost(postId);
    }

    public HostEvaluation getHostEvaluation(int evaluationId){
        HostEvaluationPO hostEvaluationPO = hostEvaluationDAO.selectByEvaluationId(evaluationId);
        if(hostEvaluationPO ==null){
            return null;
        }
        HostEvaluation hostEvaluation = new HostEvaluation();
        BeanUtils.copyProperties(hostEvaluationPO,hostEvaluation);
        return hostEvaluation;
        }

    public Integer[] getPostsByPost(int targetId, int offset, int num){
        return postDAO.selectPostByPost(targetId,offset,num);
    }

    public Integer[] getPostsByUser( int userId, int postType, int offset, int num){
        return postDAO.selectUserPostByUser(userId,postType,offset,num);
    }

    public Post getPost(int id){
        PostPO postPO = postDAO.selectById(id);
        if(postPO == null || postPO.getDeleted() == 1){
            return null;
        }
        Post post = new Post();
        BeanUtils.copyProperties(postPO,post);
        return post;
    }

    public int addPost(Post post, int userId, int postType, int hostId, int evaluation, int anonymous){
        PostPO postPO = new PostPO();
        BeanUtils.copyProperties(post,postPO);
        PostPO trend = postDAO.selectById(postPO.getTargetId());
        if(trend != null){
            trend.setCommentCount(trend.getCommentCount() + 1 );
            postDAO.updateById(trend);
            post.setType(trend.getType());
        }else if(post.getTargetId() != 0){
            throw new ParamIllegalException("无此评论！");
        }
        boolean flag = (postType == 0 && (post.getTargetId() != 0 || post.getType() == 0 || post.getType() == 5))
                || (postType == 2 && post.getType() != 0) || (postType == 3 && (post.getType() == 0)) ;
        if(flag){
            throw new ParamIllegalException("参数错误！");
        }
        postDAO.insert(postPO);
        postDAO.insertUserPost(userId,postType);
        Integer lastInsertId = postDAO.selectLastId();

        if(postType == evaluationType && post.getTargetId() == 0){
            hostEvaluationDAO.insert(hostId, evaluation, anonymous, lastInsertId);
            HostPO hostPO = hostDAO.selectById(hostId);
            if(hostPO != null){
                hostPO.setCredit(evaluation + hostPO.getCredit());
                hostDAO.updateById(hostPO);
            }
        }
        return lastInsertId;
    }

    public int deletePost(int userId,int id){
        Integer getUserId = postDAO.selectUserId(id);
        if(getUserId == null || userId != getUserId){
            return ResponseCode.PERMISSION_FORBIDDEN;
        }
        postDAO.delete(id);
        postDAO.deleteUserPost(id);
        return ResponseCode.REQUEST_SUCCESSED;
    }
}
